package com.fcscs.cservice.client.main.inroomdining;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.fcscs.core.common.MainPaginationSearchModel;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.SearchExecutor;
import com.fcscs.core.gwt.common.ui.BaseFormDialog;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.FcsPanel;
import com.fcscs.core.gwt.common.ui.IEFriendlyDateField;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.inroomdining.InRoomDiningModel;
import com.fcscs.cservice.client.common.model.restaurants.BookingStatusGWTEnum;
import com.fcscs.cservice.client.common.model.restaurants.BookingStatusModelData;
import com.fcscs.cservice.client.common.model.restaurants.BookingViewModelData;
import com.fcscs.cservice.client.common.model.restaurants.SmokingOrNotGWTEnum;
import com.fcscs.cservice.client.common.model.restaurants.SmokingOrNotGWTEnumModelData;
import com.fcscs.cservice.client.main.restaurants.GuestBookingHistoryGrid;
import com.fcscs.cservice.domain.model.BookingView;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class InRoomDiningDetailDialog extends BaseFormDialog<InRoomDiningModel> {
	private static InRoomDiningDetailDialog INSTANCE;
	private MyFormPanel myFormPanel;
	private VerticalPanel vp;
	private HorizontalPanel hp1;
	private LayoutContainer lc1;
	private HorizontalPanel hp2;
	private LayoutContainer lc2;
	private Label roomNumLab;
	private Label roomNum;
	private Label guestNameLabel;
	private Label guestName;
	private Label categoryLabel;
	private Label categoryName;
	private Label statusLabel;
	private GuestBookingHistoryGrid bookHisGrid;
	private InRoomDiningHistoryGrid inDiningGrid;
	private FormButton saveBtn;
	private FormButton cancelBtn;
	private HorizontalPanel hp3;
	private HorizontalPanel hp4;
	private HorizontalPanel hp5;
	private HorizontalPanel hp6;
	private HorizontalPanel hp7;
	private LayoutContainer lc3;
	private LayoutContainer lc4;
	private LayoutContainer lc5;
	private LayoutContainer lc6;
	private LayoutContainer lc7;
	private IEFriendlyDateField arriveDateField;
	private IEFriendlyDateField departureDateField;
	private EasyClearCombobox<SmokingOrNotGWTEnumModelData> cookOptCombox;
	private ListStore<SmokingOrNotGWTEnumModelData> smokingStore;
	private EasyClearCombobox<SmokingOrNotGWTEnumModelData> paymentCombox;
	private TextField<String> deliveryField;
	private TextField<String> grand;
	private TextField<String> dateNote;
	private TextField<String> item;
	private EasyClearCombobox<BookingStatusModelData> bookingStatusCombox;
	private ListStore<BookingStatusModelData> bookingStatusStore;
	private TextField<String> qtyField;
	private TextField<String> sub;

	// private InRoomDiningModelData bViewModel;
	// private List<BookingViewModelData> bHistoryList;

	public static InRoomDiningDetailDialog getInstance() {

		if (null == INSTANCE) {
			INSTANCE = new InRoomDiningDetailDialog();
		}
		INSTANCE.bookHisSearch();
		INSTANCE.setHeading(CServiceConstants.CSERVICE_CONSTANTS.in_RoomDiningDetail());
		return INSTANCE;
	}

	private InRoomDiningDetailDialog() {
		myFormPanel = new MyFormPanel(null);
		add(myFormPanel);
		setSize(785, 680);
	}

	public class MyFormPanel extends FcsFormPanel<InRoomDiningModel> {

		protected MyFormPanel(FormSubmitListener<InRoomDiningModel> l) {
			super(l);
			setHeaderVisible(false);

			vp = new VerticalPanel();
			vp.setSpacing(5);

			hp1 = new HorizontalPanel();
			hp1.setId("InRoomDiningDetailDialog_hp1");
			vp.add(hp1);

			hp2 = new HorizontalPanel();
			hp2.setId("InRoomDiningDetailDialog_hp2");
			vp.add(hp2);

			hp3 = new HorizontalPanel();
			hp3.setId("InRoomDiningDetailDialog_hp3");
			vp.add(hp3);

			hp4 = new HorizontalPanel();
			hp4.setId("InRoomDiningDetailDialog_hp4");
			vp.add(hp4);
			hp5 = new HorizontalPanel();
			hp5.setId("InRoomDiningDetailDialog_hp5");
			vp.add(hp5);

			hp6 = new HorizontalPanel();
			hp6.setId("InRoomDiningDetailDialog_hp2");
			vp.add(hp6);

			hp7 = new HorizontalPanel();
			hp7.setId("InRoomDiningDetailDialog_hp7");
			vp.add(hp7);

			lc1 = new LayoutContainer();
			lc1.setLayout(new ColumnLayout());
			hp1.add(lc1);

			roomNumLab = new Label(CServiceConstants.CSERVICE_CONSTANTS.roomNum()
					+ CServiceConstants.FIELD_SEP);
			roomNumLab.setId("InRoomDiningDetailDialog_roomNumLab");
			lc1.add(roomNumLab, new ColumnData(140));

			roomNum = new Label();
			roomNum.setId("InRoomDiningDetailDialog_roomNum");
			lc1.add(roomNum, new ColumnData(150));

			guestNameLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.guestName()
					+ CServiceConstants.FIELD_SEP);
			guestNameLabel.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "120px");
			guestNameLabel.setId("InRoomDiningDetailDialog_guestNameLabel");
			lc1.add(guestNameLabel, new ColumnData(140));

			guestName = new Label();
			guestName.setId("InRoomDiningDetailDialog_guestNameAF");
			lc1.add(guestName, new ColumnData(150));

			lc2 = new LayoutContainer();
			lc2.setLayout(new ColumnLayout());
			hp2.add(lc2);

			categoryLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.category()
					+ CServiceConstants.FIELD_SEP);
			categoryLabel.setId("InRoomDiningDetailDialog_categoryLabel");
			lc2.add(categoryLabel, new ColumnData(140));

			categoryName = new Label();
			categoryName.setId("InRoomDiningDetailDialog_categoryName");
			lc2.add(categoryName, new ColumnData(150));

			statusLabel = new Label(CoreConstants.CORE_CONSTANTS.status() + CServiceConstants.FIELD_SEP);
			statusLabel.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "120px");
			statusLabel.setId("InRoomDiningDetailDialog_statusLabel");
			lc2.add(statusLabel, new ColumnData(140));
			//
			// status = new Label();
			// status.setId("InRoomDiningDetailDialog_categoryNameAF");
			// lc2.add(status, new ColumnData(150));

			bookingStatusCombox = new EasyClearCombobox<BookingStatusModelData>();
			bookingStatusCombox.setId("ResBookingDetailDialog_smokingCombox");
			bookingStatusCombox.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.bookingStatus());
			bookingStatusStore = new ListStore<BookingStatusModelData>();
			for (BookingStatusGWTEnum be : BookingStatusGWTEnum.values()) {
				bookingStatusStore.add(new BookingStatusModelData(be));
			}

			bookingStatusCombox.setStore(bookingStatusStore);
			bookingStatusCombox.setDisplayField(CoreConstants.FULLTNAME);
			bookingStatusCombox.setTriggerAction(TriggerAction.ALL);
			lc2.add(bookingStatusCombox);

			lc3 = new LayoutContainer();
			lc3.setLayout(new ColumnLayout());
			hp3.add(lc3);

			Label arriveDateLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.arrivalDate()
					+ CServiceConstants.FIELD_SEP);
			arriveDateLabel.setId("InRoomDiningDetailDialog_arriveDateField");
			lc3.add(arriveDateLabel, new ColumnData(140));

			arriveDateField = new IEFriendlyDateField();
			arriveDateField.setId("ResBookingDetailDialog_arriveDateField");
			lc3.add(arriveDateField,new ColumnData(150));

			Label departureDateLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.departureDate()
					+ CServiceConstants.FIELD_SEP);
			departureDateLabel.setId("InRoomDiningDetailDialog_departureDateLabel");
			departureDateLabel.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "120px");
			lc3.add(departureDateLabel, new ColumnData(140));

			departureDateField = new IEFriendlyDateField();
			departureDateField.setId("ResBookingDetailDialog_departureDateField");
			lc3.add(departureDateField,new ColumnData(150));

			lc4 = new LayoutContainer();
			lc4.setLayout(new ColumnLayout());
			hp4.add(lc4);

			Label qtyLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.qty()
					+ CServiceConstants.FIELD_SEP);
			qtyLabel.setId("InRoomDiningDetailDialog_qtyLabel");
			lc4.add(qtyLabel, new ColumnData(140));

			qtyField = new TextField<String>();
			qtyField.setId("InRoomDiningDetailDialog_qtyField");
			lc4.add(qtyField, new ColumnData(150));

			Label optionLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.cookOption()
					+ CServiceConstants.FIELD_SEP);
			optionLabel.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "120px");
			optionLabel.setId("InRoomDiningDetailDialog_optionLabel");
			lc4.add(optionLabel, new ColumnData(140));

			cookOptCombox = new EasyClearCombobox<SmokingOrNotGWTEnumModelData>();
			cookOptCombox.setId("ResBookingDetailDialog_cookOptCombox");
			cookOptCombox.setDisplayField(CoreConstants.FULLTNAME);
			smokingStore = new ListStore<SmokingOrNotGWTEnumModelData>();
			for (SmokingOrNotGWTEnum sone : SmokingOrNotGWTEnum.values()) {
				smokingStore.add(new SmokingOrNotGWTEnumModelData(sone));
			}
			cookOptCombox.setStore(smokingStore);
			cookOptCombox.setTriggerAction(TriggerAction.ALL);
			lc4.add(cookOptCombox, new ColumnData(150));

			lc5 = new LayoutContainer();
			lc5.setLayout(new ColumnLayout());
			hp5.add(lc5);

			Label paymentLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.paymentMethod()
					+ CServiceConstants.FIELD_SEP);
			paymentLabel.setId("InRoomDiningDetailDialog_paymentLabel");
			lc5.add(paymentLabel, new ColumnData(140));

			paymentCombox = new EasyClearCombobox<SmokingOrNotGWTEnumModelData>();
			paymentCombox.setId("ResBookingDetailDialog_paymentCombox");
			paymentCombox.setDisplayField(CoreConstants.FULLTNAME);
			smokingStore = new ListStore<SmokingOrNotGWTEnumModelData>();
			for (SmokingOrNotGWTEnum sone : SmokingOrNotGWTEnum.values()) {
				smokingStore.add(new SmokingOrNotGWTEnumModelData(sone));
			}
			paymentCombox.setStore(smokingStore);
			paymentCombox.setTriggerAction(TriggerAction.ALL);
			lc5.add(paymentCombox, new ColumnData(150));

			Label deliveryLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.deliveryLocation()
					+ CServiceConstants.FIELD_SEP);
			deliveryLabel.setId("InRoomDiningDetailDialog_deliveryLabel");
			deliveryLabel.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "120px");
			lc5.add(deliveryLabel, new ColumnData(140));

			deliveryField = new TextField<String>();
			deliveryField.setId("InRoomDiningDetailDialog_deliveryField");
			lc5.add(deliveryField, new ColumnData(150));

			lc6 = new LayoutContainer();
			lc6.setLayout(new ColumnLayout());
			hp6.add(lc6);

			Label subLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.subTotal()
					+ CServiceConstants.FIELD_SEP);
			subLabel.setId("InRoomDiningDetailDialog_subLabel");
			lc6.add(subLabel, new ColumnData(140));

			sub = new TextField<String>();
			sub.setId("InRoomDiningDetailDialog_sub");
			lc6.add(sub, new ColumnData(150));

			Label grandLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.grandTotal()
					+ CServiceConstants.FIELD_SEP);
			grandLabel.setId("InRoomDiningDetailDialog_grandLabel");
			grandLabel.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "120px");
			lc6.add(grandLabel, new ColumnData(140));

			grand = new TextField<String>();
			grand.setId("InRoomDiningDetailDialog_grand");
			lc6.add(grand, new ColumnData(150));

			lc7 = new LayoutContainer();
			lc7.setLayout(new ColumnLayout());
			hp7.add(lc7);

			Label dateNoteLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.dateNote()
					+ CServiceConstants.FIELD_SEP);
			dateNoteLabel.setId("InRoomDiningDetailDialog_dateNoteLabel");
			lc7.add(dateNoteLabel, new ColumnData(140));

			// dateNote = new Label();
			// dateNote.setId("InRoomDiningDetailDialog_sub");
			// lc7.add(dateNote, new ColumnData(150));

			dateNote = new TextField<String>();
			dateNote.setId("InRoomDiningDetailDialog_dateNote");
			lc7.add(dateNote, new ColumnData(150));

			Label itemLabel = new Label(CoreConstants.CORE_CONSTANTS.item() + CServiceConstants.FIELD_SEP);
			itemLabel.setId("InRoomDiningDetailDialog_itemLabel");
			itemLabel.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "120px");
			lc7.add(itemLabel, new ColumnData(140));

			item = new TextField<String>();
			item.setId("InRoomDiningDetailDialog_item");
			lc7.add(item, new ColumnData(150));

			bookHisGrid = new GuestBookingHistoryGrid(CServiceConstants.CSERVICE_CONSTANTS.bookingHistory(),
					new SearchExecutor<BookingViewModelData, MainPaginationSearchModel>() {

						@Override
						public void doSearch(final MainPaginationSearchModel searchModel,
								final AsyncCallback<BasePagingLoadResult<BookingViewModelData>> callback) {
							// final long propId =
							// bViewModel.getEntity().getPropertyId();
							// final long guestId = bViewModel.getEntity().get;
							// BookingServiceController.getInstance().getBookingHistory(propId,
							// guestId, 0, 20,
							// new
							// AsyncCallbackListener<SearchResultsContainer<BookingView>>()
							// {
							//
							// @Override
							// public void onLogin(String username) {
							// BookingServiceController.getInstance().getBookingHistory(propId,
							// guestId, 0, 20, this);
							// }
							//
							// @Override
							// public void
							// onSuccess(SearchResultsContainer<BookingView>
							// result) {
							// if (null != result) {
							// bHistoryList = new
							// ArrayList<BookingViewModelData>();
							// for (BookingView bv : result.getResults()) {
							// bHistoryList.add(new BookingViewModelData(bv));
							// }
							// callback.onSuccess(FcsPanel.createPagingLoadResult(
							// bHistoryList, searchModel.getCurrentPage(),
							// searchModel.getResultsPerPage(),
							// result.getTotalCnt()));
							// }
							// }
							//
							// @Override
							// public void onFailure(Throwable caught) {
							// MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(
							// CoreConstants.CORE_CONSTANTS.system(),
							// ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
							// CoreConstants.CORE_CONSTANTS.systemError(),
							// null);
							// }
							// });

							List<BookingViewModelData> bookHisList = new ArrayList<BookingViewModelData>();
							BookingView model1 = new BookingView();
							model1.setReservationDate(new Date());
							model1.setRestaurantName("restaurant1");
							model1.setNote("window sit");
							model1.setStatus("completed");

							bookHisList.add(new BookingViewModelData(model1));

							BookingView model2 = new BookingView();
							model2.setReservationDate(new Date());
							model2.setRestaurantName("restaurant2");
							model2.setNote("window sit2");
							model2.setStatus("completed2");

							bookHisList.add(new BookingViewModelData(model2));
							callback.onSuccess(FcsPanel.createPagingLoadResult(bookHisList,
									searchModel.getCurrentPage(), searchModel.getResultsPerPage(),
									bookHisList.size()));

						}
					});
			bookHisGrid.setFrame(false);
			bookHisGrid.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "10px");
			vp.add(bookHisGrid);

			inDiningGrid = new InRoomDiningHistoryGrid(
					CServiceConstants.CSERVICE_CONSTANTS.in_RoomDiningHistory(),
					new SearchExecutor<BookingViewModelData, MainPaginationSearchModel>() {

						@Override
						public void doSearch(MainPaginationSearchModel searchModel,
								AsyncCallback<BasePagingLoadResult<BookingViewModelData>> callback) {

						}
					});
			inDiningGrid.setFrame(false);
			vp.add(inDiningGrid);

			saveBtn = new FormButton(CoreConstants.CORE_CONSTANTS.save());
			saveBtn.setId("PublicAreaEConnectDialog_saveBtn_Id");
			getButtonBar().add(saveBtn);

			cancelBtn = new FormButton(CoreConstants.CORE_CONSTANTS.cancel());
			cancelBtn.setId("PublicAreaEConnectDialog_cancelBtn_Id");
			cancelBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
					InRoomDiningDetailDialog.this.hide();
					cancelBtn.removeStyleName(CoreConstants.CSS_FORM_BUTTON_FOCUS);
				}
			});
			getButtonBar().add(cancelBtn);

			add(vp);

		}

		@Override
		public void updateForm(InRoomDiningModel entity) {
			 roomNum.setText(entity.getRoomNo());
			 guestName.setText(entity.getGuestName());
			// categoryName.setText(entity.getCategroy());
			// status.setText(entity.getStatus());
		}

		@Override
		public void populateEntity(InRoomDiningModel entity) {

		}

		@Override
		public void setFocus() {
			bookingStatusCombox.focus();
		}

	}

	private void bookHisSearch() {
		String term = null;
		MainPaginationSearchModel sm = new MainPaginationSearchModel();
		sm.setSearchTerm(term);
		bookHisGrid.newSearch(sm);
	}

	@Override
	public void setFocus() {

	}

	@Override
	protected FcsFormPanel<InRoomDiningModel> getFormPanel() {
		return myFormPanel;
	}

}
